package com.gyh.stlserver.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gyh.stlserver.constants.Constants;
import com.gyh.stlserver.entity.Admin;
import com.gyh.stlserver.service.AdminService;
import com.gyh.stlserver.util.JWTUtil;
import com.gyh.stlserver.util.Result;
import com.gyh.stlserver.vo.AdminVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /*获取所有管理员信息*/
    @GetMapping
    public Result getAdminAll(Integer page,Integer limit,Admin admin){
        /*page和limit为空查寻所有*/
        if (ObjectUtil.isEmpty(page)&&ObjectUtil.isEmpty(limit)){
            List<Admin> admins = adminService.getAdminAll(admin);
            return Result.success(Constants.STATUS_200,admins);
        }
        /*不为空分页查询*/
        return Result.success(Constants.STATUS_200,adminService.getAdminByPage(page,limit,admin));
    }/*获取登录信息*/
    @GetMapping("/getLogin")
    public Result getLogin(HttpServletRequest request){
        String token = request.getHeader(Constants.TOKEN);
        Claims claims = null;
        try {
            claims = JWTUtil.parseJWT(token);
        } catch (Exception e) {
            return Result.error(Constants.STATUS_401,"未登录");
        }
        String phone = claims.getSubject();
        Admin admin = adminService.getAdminByPhone(phone);
        return Result.success(Constants.STATUS_200,null,admin);
    }
    /*添加管理员*/
    @PostMapping("/addAdmin")
    public Result addAdmin(@RequestBody Admin admin){
        if (ObjectUtil.isEmpty(admin)){
            Result.error(Constants.STATUS_401,"用户不能为空");
        }
        int i = adminService.addAdmin(admin);
        if (i<1){
            return Result.error(Constants.STATUS_401,"用户添加失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*添加管理员*/
    @GetMapping("/checkPhone")
    public Result checkPhone(String phone){
        Admin admin = adminService.checkPhone(phone);
        if (ObjectUtil.isNotEmpty(admin)){
           return Result.error(Constants.STATUS_402,"手机号存在");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*修改管理员信息*/
    @PostMapping("/modifyAdmin")
    public Result modifyAdmin(@RequestBody Admin admin){
        if (ObjectUtil.isEmpty(admin)){
            return Result.error(Constants.STATUS_401,"参数为空");
        }
        int i = adminService.modifyAdmin(admin);
        if (i<1){
            return Result.error(Constants.STATUS_401,"更新失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    /*删除管理员*/
    @GetMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestParam Integer id){
        if (ObjectUtil.isEmpty(id)){
            return Result.error(Constants.STATUS_401,"用户id为空");
        }
        int i = 0;
        try {
            i = adminService.deleteAdmin(id);
        }catch (Exception e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }
        if (i<1){
            return Result.error(Constants.STATUS_401,"用户删除失败");
        }
        return Result.success(Constants.STATUS_200);
    }

    @PostMapping("/delSelected")
    public Result delSelected(@RequestBody Integer[] ids){
        if (ObjectUtil.isEmpty(ids)){
            return Result.error(Constants.STATUS_401,"ids为空");
        }
        int i = adminService.delSelected(ids);
        if (i<1){
            Result.error(Constants.STATUS_401,"删除异常");
        }
        return Result.success(Constants.STATUS_200);
    }



    @PostMapping("/phone")
    public Admin getPhone(String phone){
        return adminService.getAdminByPhone(phone);
    }
    @PostMapping("/login")
    public Result login(@RequestBody AdminVo adminVo){
        /*验证码是否为空*/
        if (StrUtil.isEmpty(adminVo.getCaptcha())){
            return Result.error(Constants.STATUS_401,"验证码不能为空");
        }
        /*验证码是否过时*/
        try {
            Claims claims = JWTUtil.parseJWT(adminVo.getKey());
            String oldCaptcha = claims.getSubject();
            /*判断验证码是否正确*/
            if (!oldCaptcha.equalsIgnoreCase(adminVo.getCaptcha())){
                return Result.error(Constants.STATUS_401,"验证码不正确");
            }
        } catch (Exception e) {
            return Result.error(Constants.STATUS_401,"验证码失效");
        }
        String token = null;
        Admin admin = null;
        try {
            admin = adminService.login(adminVo);
            if (ObjectUtil.isEmpty(admin)) {
                return Result.error(Constants.STATUS_401,"账号或者密码错误");
            }
            token = JWTUtil.createJWT(admin.getPhone(),60*60*1000L);
        }catch (Exception e){
            return Result.error(Constants.STATUS_401,e.getMessage());
        }
        return Result.success(200,token,admin);
    }
}
